package com.goods.business.service.impl;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author jiaoyanlin
 * @Date 2021/11/10 11:26
 * @Description inStockServiceImpl
 */
@Service
public class InStockServiceImpl implements InStockService {
    @Autowired
    private InStockMapper inStockMapper;
    @Override
    public PageVO<InStockVO> findInStockList(Map<String, Object> map) {
        Integer pageNum = Convert.toInt(map.get("pageNum"), 1);
        Integer pageSize = Convert.toInt(map.get("pageSize"), 10);
        Integer status = Convert.toInt(map.get("status"), 0);
        Object inNum1 = map.get("intNum");
        Object startTime = map.get("startTime");
        Object endTime = map.get("endTime");
        PageHelper.startPage(pageNum,pageSize);
        Example example =new Example(InStock.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",status);
         String inNum="";
       if (null!=inNum1){
          inNum=Convert.toStr(inNum1);
          criteria.andLike("in_Num","%"+inNum+"%");
       }

       if (null!=startTime&&null!=endTime){
           criteria.andBetween("createTime",startTime,endTime);
       }

        List<InStock> inStocks = inStockMapper.selectByExample(example);
      List<InStockVO> inStockVOS =  inStocks.stream().map(inStock -> {
          InStockVO inStockVO = new InStockVO();
          BeanUtils.copyProperties(inStock,inStockVO);
            return inStockVO;
        }).collect(Collectors.toList());

        List<InStockVO> page = ListPageUtils.page(inStockVOS, pageSize, pageNum);
        PageInfo<InStockVO> info = new PageInfo<>(inStockVOS);

        return new PageVO<>(info.getTotal(),page);
    }
   // TODO 前端到底需要返回个 什么类型的数据呢？
    @Override
    public PageVO<InStockVO> detail(Long id, Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        Example example =new Example(InStock.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        List<InStock> inStocks = inStockMapper.selectByExample(example);
        List<InStockVO> inStockVOS =  inStocks.stream().map(inStock -> {
            InStockVO inStockVO = new InStockVO();
            BeanUtils.copyProperties(inStock,inStockVO);
            return inStockVO;
        }).collect(Collectors.toList());

        List<InStockVO> page = ListPageUtils.page(inStockVOS, 10, pageNum);
        PageInfo<InStockVO> info = new PageInfo<>(inStockVOS);

        return new PageVO<>(info.getTotal(),page);
    }

    @Override
    public void remove(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(1);
        inStockMapper.updateByPrimaryKey(inStock);
    }

    @Override
    public void back(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
    }

    @Override
    public void delete(Long id) {
        inStockMapper.deleteByPrimaryKey(id);
    }
    // TODO  怎么 把map中的值 封装进 实体类对象中
    @Override
    public void addIntoStock(Map<String, Object> map) {
        Object type = map.get("type");
        Object sort = map.get("sort");
        Object valueProvince = map.get("valueProvince");
        Object valueCity = map.get("valueCity");
        Object city = map.get("city");
        Object valueOrigin = map.get("valueOrigin");
        Object origin = map.get("origin");
        Object province = map.get("province");
        Object name = map.get("name");
        Object contact = map.get("contact");
        Object email = map.get("email");
        Object phone = map.get("phone");
        Object remark = map.get("remark");
        Object address = map.get("address");
        // TODO products 长什么样
        Object products = map.get("products");
        System.out.println("products = " + products);
        InStock inStock = new InStock();

        //判断
        if (null!=type){
          inStock.setType(Convert.toInt(type));
        }
       /* if (null!=sort){

        }*/
        if (null!=remark){
          inStock.setRemark(Convert.toStr(remark));
        }
      /*  if (null!=type){

        }*/
      inStock.setStatus(2);//新添加的是 待审核
        inStockMapper.insert(inStock);
    }

    @Override
    public void publish(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
    }

}
